package org.itsolutions.javaBackup.ioc;

import java.io.Serializable;
import java.lang.annotation.Annotation;

import org.itsolutions.javaBackup.spi.EModule;

/**
 * Implementation of InjectionID annotation used in ARTTPModule. 
 * @author Tomas Fecko
 */
final class InjectionIDImpl implements InjectionID, Serializable {

	static final String 		DEFAULT_STRING = "default";
	private static final long 	serialVersionUID = -2228965404019280594L;
	private final Class<?>		mValue;
	private final String		mID;
	private final EModule		mModule;
	
	/**
	 * 
	 * @param pClass identifier class
	 * @param pValue identifier string
	 */
	public InjectionIDImpl(Class<?> pClass, String pValue, EModule pModule) {
		mValue = (pClass != null) ? pClass :  DefaultInjectionIDClass.class;
		mID = (pValue != null) ? pValue : DEFAULT_STRING;
		mModule = (pModule != null) ? pModule : EModule.UNSPECIFIED;
	}
	
	/**
	 * 
	 * @param pClass identifier class
	 */
	public InjectionIDImpl(Class<?> pClass) {
		mValue = (pClass != null) ? pClass :  DefaultInjectionIDClass.class;
		mID = DEFAULT_STRING;
		mModule = EModule.UNSPECIFIED;
	}

	/**
	 * 
	 * @param pValue identifier string
	 */
	public InjectionIDImpl(String pValue) {
		mID = (pValue != null) ? pValue : DEFAULT_STRING;
		mValue = DefaultInjectionIDClass.class;
		mModule = EModule.UNSPECIFIED;
	}
	
	/**
	 * 
	 * @param pValue identifier string
	 */
	public InjectionIDImpl(EModule pModule) {
		mID = DEFAULT_STRING;
		mValue = DefaultInjectionIDClass.class;
		mModule = (pModule != null) ? pModule : EModule.UNSPECIFIED;
	}

	@Override
	public Class<? extends Annotation> annotationType() {
	    return InjectionID.class;
	}

	@Override
	public Class<?> value() {
		return mValue;
	}

	@Override
	public String id() {
		return mID;
	}
	
	@Override
	public EModule module() {
		return mModule;
	}

	/**
	 * Default empty impl.
	 */
	static class DefaultInjectionIDClass {
	}

	@Override
	public int hashCode() {
	    // This is specified in java.lang.Annotation: 
		// http://download.oracle.com/javase/1.5.0/docs/api/java/lang/annotation/Annotation.html#hashCode
		//CHECKSTYLE:OFF
		return (127 * "value".hashCode() ^ mValue.hashCode()) 
		+ (127 * "id".hashCode() ^ mID.hashCode())
		+ (127 * "module".hashCode() ^ mModule.hashCode());		
	    //CHECKSTYLE:ON		
	}

	@Override
	public boolean equals(Object pObj) {
		if (this == pObj) {
			return true;
		}
		if (pObj == null) {
			return false;
		}
		if (!(pObj instanceof InjectionIDImpl)) {
			return false;
		}
		InjectionIDImpl other = (InjectionIDImpl) pObj;
		if (mID == null) {
			if (other.mID != null) {
				return false;
			}
		} else if (!mID.equals(other.mID)) {
			return false;
		}
		if (mModule != other.mModule) {
			return false;
		}
		if (mValue == null) {
			if (other.mValue != null) {
				return false;
			}
		} else if (!mValue.equals(other.mValue)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("InjectionIDImpl [mValue=");
		builder.append(mValue.getSimpleName());
		builder.append(", mID=");
		builder.append(mID);
		builder.append(", mModule=");
		builder.append(mModule);
		builder.append("]");
		return builder.toString();
	}
}




